

//
//  PPAlertView.m
//  PPAlertView
//
//  Created by macfai on 16/3/1.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#import "PPAlertView.h"

#define kScreenHeight  [UIScreen mainScreen].bounds.size.height
#define kScreenWidth  [UIScreen mainScreen].bounds.size.width
#define kAlertH 140
#define kAlertW kScreenWidth*0.7

@interface PPAlertView()


@property(nonatomic,strong)UIView *alertView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *mesLabel;
@property(nonatomic,strong)UIView *lineViewH;
@property(nonatomic,strong)UIView *lienViewV;



@end

@implementation PPAlertView

//这个只能是+方法时 才能用 self alloc
+(void)showWithTitle:(NSString *)title message:(NSString *)message leftText:(NSString *)leftText rightText:(NSString *)rightText leftBlock:(PPLeftBlock)leftBlock rightBlock:(PPRightBlock)rightBlock{
    
    PPAlertView *selfView = [[self alloc]initWithFrame:[UIScreen mainScreen].bounds];
    selfView.backgroundColor = [UIColor colorWithRed:0.50 green:0.50 blue:0.50 alpha:0.3];
    [[UIApplication sharedApplication].keyWindow addSubview:selfView];
    
    
    [selfView contentViewWithTipText:title descText:message LeftText:leftText rightText:rightText leftBlock:leftBlock RightBlock:rightBlock];
    
}

- (void)contentViewWithTipText:(NSString *)tipText descText:(NSString *)descText LeftText:(NSString *)leftText rightText:(NSString *)rightText leftBlock:(PPLeftBlock)leftBlock RightBlock:(PPRightBlock)rightBlock
{
    
    
    self.leftBlock = leftBlock;
    self.rightBlock = rightBlock;
    
    UIView *alertView = [UIView new];
    alertView.frame = CGRectMake(0, 0, kAlertW, kAlertH);
    alertView.layer.cornerRadius = 10.f;
    alertView.layer.masksToBounds = YES;
    alertView.center = self.center;
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.alpha = 0;
    alertView.transform = CGAffineTransformMakeScale(0.01, 0.01);
    [self addSubview:alertView];
    
    
    [UIView animateWithDuration:.3f animations:^{
        alertView.alpha = 1.0;
        alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
        alertView.transform = CGAffineTransformIdentity;
    }];
    
    
    UILabel *tipLabel = [UILabel new];
    tipLabel.frame = CGRectMake(0, 20, alertView.frame.size.width, 30);
    tipLabel.text = tipText;
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.font = [UIFont systemFontOfSize:15];
    [alertView addSubview:tipLabel];
    
    
    UILabel *descLabel = [UILabel new];
    descLabel.frame = CGRectMake(0, 38, alertView.frame.size.width, 60);
    descLabel.textAlignment = NSTextAlignmentCenter;
    descLabel.text = descText;
    descLabel.numberOfLines =0;
    descLabel.font = [UIFont systemFontOfSize:14];
    [alertView addSubview:descLabel];
    
    UIView *lineHView = [UIView new];
    lineHView.frame = CGRectMake(0, 100, alertView.frame.size.width, 0.5);
    lineHView.backgroundColor = [UIColor colorWithRed:0.86 green:0.86 blue:0.87 alpha:1];
    [alertView addSubview:lineHView];
    
    UIView *lineVView = [UIView new];
    lineVView.frame = CGRectMake(alertView.frame.size.width * 0.5, CGRectGetMaxY(lineHView.frame), 0.5, alertView.frame.size.height -CGRectGetMaxY(lineHView.frame));
    lineVView.backgroundColor = [UIColor colorWithRed:0.86 green:0.86 blue:0.87 alpha:1];
    [alertView addSubview:lineVView];
    
    
    UIButton *sureBtn = [UIButton new];
    sureBtn.frame = CGRectMake(alertView.frame.size.width/2 , 100, alertView.frame.size.width / 2, 40);
    [sureBtn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setTitle:rightText forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor colorWithRed:0.19 green:0.62 blue:0.78 alpha:1] forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [alertView addSubview:sureBtn];
    
    
    UIButton *cancelBtn = [UIButton new];
    cancelBtn.frame = CGRectMake(0, 100, alertView.frame.size.width * 0.5, 40);
    [cancelBtn setTitle:leftText forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor colorWithRed:0.19 green:0.62 blue:0.78 alpha:1] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    cancelBtn.backgroundColor = [UIColor clearColor];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [alertView addSubview:cancelBtn];
    
    
    
}

- (void)sureBtnClick:(UIButton *)btn
{
    self.rightBlock();
    [self dismiss];
}

- (void)cancelBtnClick:(UIButton *)btn
{
    
    self.leftBlock();
    [self dismiss];
}

-(void)dismiss
{
    [UIView animateWithDuration:0.3f animations:^{
        [self.subviews objectAtIndex:0].transform = CGAffineTransformMakeScale(0.01, 0.01);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
